﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;


namespace BookClubIt
{
    public partial class editprofile : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                BindData();
            }

        }

        private void BindData()
        {
            DTO.Member member = MemberProcessorInstance.GetMemberById(CurrentUserId);

            txtUsername.Text = member.Username;
            txtEmail.Text = member.Email.ToString();
            txtBio.InnerText = member.Biography.ToString();
            imgUserAvatar.ImageUrl = "../avatars/" + member.AvatarURL.ToString();
            //  lblAccountID.Text = member.Id.ToString();
            //  lblUsername.Text = username.ToString();

            int notifID = member.NotificationId;

            rblNotifications.SelectedIndex = notifID - 1;
        }

        protected void btnEditProfile_Click(object sender, EventArgs e)
        {
            //DTO.Member member = new DTO.Member
            //{
            //    Username = txtUsername.Text,
            //    Email = txtEmail.Text,
            //    CreatedDate = DateTime.Now,
            //    FirstName = string.Empty,
            //    LastName = string.Empty,
            //    Biography = txtBio.InnerText,
            //    Zipcode = string.Empty,
            //    AvatarURL = "default_avatar.png",
            //    NotificationId = 1,
            //    Id = CurrentUserId;
            //};

           //  MemberProcessorInstance.SaveMember(member);

        }
        protected void btnSaveNotifications_Click(object sender, EventArgs e)
        {
            string username = HttpContext.Current.User.Identity.Name.ToString();
            DTO.Member member = MemberProcessorInstance.GetMemberByUsername(username);
            int CurrentUserId = member.Id;

            MemberProcessorInstance.UpdateMemberNotification(CurrentUserId, Convert.ToInt32(rblNotifications.SelectedItem.Value));

            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string username = HttpContext.Current.User.Identity.Name.ToString();
            DTO.Member member = MemberProcessorInstance.GetMemberByUsername(username);
            int CurrentUserId = member.Id;


           
            string filename = UploadImage.FileName;
            string filepath = filename;
            if (UploadImage.PostedFile.ContentLength != 0)
            {
                UploadImage.SaveAs(Server.MapPath("~/avatars/" + filename));
                MemberProcessorInstance.UpdateMemberAvatar(CurrentUserId, filepath);
                BindData();

            }
        }

     


    }

}